package com.qdlc.p2p.biz.service.activity;

import java.util.Date;

import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.dto.NewActivityRule;

public abstract class AbstractActivityProgrammeHandler<T extends ActivityProgrammeParam> implements ActivityProgrammeHandler<T> {

	@Override
	public void handle(NewActivityRule activityProgramme, T param) throws ActivityPlanException{
		boolean check = preperCheck(activityProgramme, param);
		if(check){
			check = preperCheckInternal(param);
		}
		if(check){
			handleInternal(param);
		}
	}
	
	private boolean preperCheck(NewActivityRule activityProgramme, T param){
        
		if (activityProgramme == null) {
            return false;
        }
		
		if(param.getUser() == null){
			throw new IllegalArgumentException("++++ please set user field for ActivityProgrammeParam");
		}
        
        // 活动未激活状态,0:开启规则，1:关闭规则
        int status = activityProgramme.getStatus();
        if (status != 0) {
            return false;
        }

        //数据库表中必须要填写清楚时间,如果为空则校验不通过
        Date start_time = activityProgramme.getStartTime();
        Date end_time = activityProgramme.getEndTime();
        if (start_time == null || end_time == null) {
            return false;
        }
        
        //开始结束时间判定
        Date date = DateUtil.getNow();
        if (start_time.getTime() > date.getTime()
                || end_time.getTime() < date.getTime()) {
            return false;
        }
        
        return true;
	}
	
	/**
	 * 子类实现前置条件检测(钩子方法)
	 * @param activityProgramme
	 * @param param
	 * @return
	 */
	protected boolean preperCheckInternal(ActivityProgrammeParam param){
		return true;
	}
	

	/**
	 * 子类实现活动方案处理
	 * @param param
	 */
	protected abstract void handleInternal(T param) throws ActivityPlanException;
	
	
	/**
	 * 拷贝参数基本数据
	 * @param src
	 * @param dest
	 */
	protected void copyForm(ActivityProgrammeParam src, ActivityProgrammeParam dest){
		dest.setUser(src.getUser());
		dest.setRuleType(src.getRuleType());
		dest.setRuleId(src.getRuleId());
		dest.setActivityPlan(src.getActivityPlan());
	}
}
